package slerp;

import java.util.LinkedList;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Pasta
 */
public class Player extends Drawable {
    private boolean movingLeft, movingRight;
    double boxSizeX, boxSizeY, speed, baseSpeed, maxSpeed, acceleration, deathTimer = 0;
    Gravity g;
    HerpEngineTime time;
    Keymap keys;
    DColor c;
    public Player() {
        c = new DColor(1.f, 1.f, 1.f, 1.f);
        g = new Gravity(10);
        keys = new Keymap();
        x = 0;
        y = 0;
        boxSizeX = 20;
        boxSizeY = 20;
        baseSpeed = 10;
        acceleration = 100;
        maxSpeed = 100;
        time = new HerpEngineTime();
        time.start();
        g.startFalling();
    }
    
    public void logic() {
        double delta = time.getDeltaTime() / Math.pow(10, 9);
        keys.getInput();
        move(delta);
        death(delta);
    }

    
    public void collision(LinkedList<Block> blocks) {
        boolean botCollision = false;
        double offSetDistance = 20;
        for(Block b : blocks) {
            if(x + boxSizeX / 2 > b.x && y + boxSizeY / 2 > b.y && y - boxSizeY / 2 < b.y1 && Math.abs((x + boxSizeX / 2) - b.x) < offSetDistance) {
                speed = 0;
                System.out.println("Left");
            } else if(x - boxSizeX / 2 < b.x1 && y + boxSizeY / 2 > b.y && y - boxSizeY / 2 < b.y1 && Math.abs((x - boxSizeX / 2) - b.x1) < offSetDistance) {
                speed = 0;
                System.out.println("Right");
            } 
            if(y - boxSizeY / 2 < b.y1 && x + boxSizeX / 2 > b.x && x - boxSizeX / 2 < b.x1 && Math.abs((y - boxSizeY / 2) - b.y1) < offSetDistance) {
                y = b.y1 + boxSizeY / 2;
                System.out.println("Bot");
                botCollision = true;
                g.stopFalling();
            } else if(y + boxSizeY / 2 > b.y && x + boxSizeX / 2 > b.x && x - boxSizeX / 2 < b.x1 && Math.abs((y + boxSizeY / 2) - b.y) < offSetDistance) {
                System.out.println("Top");
                g.stopFalling();
                y = b.y - boxSizeY / 2;
            }
        }
        if(!botCollision) {
                g.startFalling();
        }
    }
    private void move(double deltaTime) {
        sprint();
        moveX(deltaTime * speed);
        updateSpeed(deltaTime);
        if(keys.getPressed().contains(Keymap.Key.MOVE_LEFT) && !keys.getPressed().contains(Keymap.Key.MOVE_RIGHT)) {
            movingRight = true;
            movingLeft = false;
        } else if(keys.getPressed().contains(Keymap.Key.MOVE_RIGHT) && !keys.getPressed().contains(Keymap.Key.MOVE_LEFT)) {
            movingLeft = true;
            movingRight = false;
        } else {
            movingLeft = false;
            movingRight = false;
        }
        if(g.isFaling()) {
            moveY(-g.getFallingSpeed());
        } else {
            jump();
        }
    }
    private void updateSpeed(double deltaTime) {
        if(movingLeft) {
            if(speed == 0) {
                speed = baseSpeed;
            }
            if(speed < maxSpeed) {
                speed += acceleration * 5 * deltaTime;
                if(speed > maxSpeed) {
                    speed = maxSpeed;
                }
            } else if(speed > maxSpeed) {
                speed -= acceleration * 5 * deltaTime;
                if(speed < maxSpeed) {
                    speed = maxSpeed;
                }
            }
        } else if (movingRight){
            if(speed == 0) {
                speed = -baseSpeed;
            }
            if(speed > -maxSpeed) {
                speed -= acceleration * 5 * deltaTime;
                if(speed < -maxSpeed) {
                    speed = -maxSpeed;
                }
            } else if(speed < -maxSpeed) {
                speed += acceleration * deltaTime;
                if(speed > -maxSpeed) {
                    speed = -maxSpeed;
                }
            }
        } else {
            if(speed > 0) {
                speed -= acceleration * deltaTime;
                if(speed < 0) {
                    speed = 0;
                }
            } else if(speed < 0) {
                speed += acceleration * deltaTime;
                if(speed > 0) {
                    speed = 0;
                }
            }
        }
    }
    private void sprint() {
        if(keys.getPressed().contains(Keymap.Key.SPRINT)) {
            maxSpeed = 500;
        } else {
            maxSpeed = 200;
        }
    }
    private void death(double time) {
        deathTimer = time / Math.pow(10, 9);
        if(deathTimer < 1) {
            c.setColor(1.f, 0.f, 0.f, 1.f);
        } else {
            c.setColor(1.f, 0.f, 0.f, 0.5f);
        }
    }
    private void jump() {
        if(keys.getPressed().contains(Keymap.Key.JUMP)) {
            g.setUpvardsVelocity(400);
            g.startFalling();
        }
    }
    private void moveY(double y) {
        this.y += y;
    }
    private void moveX(double x) {
        this.x += x;
    }
    @Override
    void draw() {
        c.getColor();
        GL11.glBegin(GL11.GL_QUADS);
           GL11.glVertex2d(x - boxSizeX / 2, y - boxSizeY / 2);
           GL11.glVertex2d(x - boxSizeX / 2, y + boxSizeY / 2);
           GL11.glVertex2d(x + boxSizeX / 2, y + boxSizeY / 2);
           GL11.glVertex2d(x + boxSizeX / 2, y - boxSizeY / 2);
        GL11.glEnd();
    }
}
